<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\api\model;
use app\api\service\Token;
use app\lib\enum\PageEnum;
use app\api\model\CouponLog;

/**
 * 优惠券
 * Class Coupon
 * @package app\api\model
 */
class Coupon extends BaseModel
{
    /**
     * @Desc 格式化日期
     *
     * @param $value
     *
     * @return false|string
     */
    protected function getCreateTimeAttr($value)
    {
        return date('Y-m-d', $value);
    }


    /**
     * @Desc 格式化日期
     *
     * @param $value
     *
     * @return false|string
     */
    protected function getEndTimeAttr($value)
    {
        return ['val'=>$value,'str'=>date('Y年m月d日 H:i:s', $value)];
    }

    /**
     * @Desc 格式化优惠券名称
     *
     * @param $value
     *
     * @return string
     */
    protected function getTitleAttr($value)
    {
        if(mb_strlen($value) > 27){
            $value = mb_substr($value,0,27);
            $value .= '...';
        }
        return $value;
    }


    /**
     * @Desc 查询优惠券列表
     *
     * @param int $type
     * @param int $page
     *
     * @return array
     */
    public static function getCouponList($type = '0', $page = 1)
    {
        $id = Token::getUidFromCache();
        // 分页数量
        $listRows = PageEnum::COUPON;
        $couponlogObj = new CouponLog();

        if($type == '0'){
            // 查询待领取列表(coupon)
            $couponObj = new Coupon();

            $builsqlStr = $couponlogObj->field('use_status')->where('user_weixin_id = '.$id.' and coupon.id = coupon_id')->limit(1)->buildSql();
            $list = $couponObj->alias('coupon')
                ->field('id, title, facevalue, end_time endtime, '.$builsqlStr.' as use_status')
                ->where(['end_time'=>['>',time()], 'app'=>['<',3], 'status'=>1])
                ->order('coupon.create_time desc')
                ->page($page, $listRows)
                ->select();

            if($list){
                foreach ($list as $key=> $item) {
                    if($item['use_status']){
                        // 有记录表示 已领取
                        $list[$key]['status'] = 2;
                    }else{
                        $list[$key]['status'] = 1;
                    }
                }
            }else{
                $list = [];
            }
        }else{
            // 查询已领取列表(coupon_log)
            $list = $couponlogObj->alias('log')
                ->field('log.id, coupon.title, coupon.facevalue, use_end_time endtime, use_status status')
                ->join('coupon coupon','coupon.id=log.coupon_id','left')
                ->where(['user_weixin_id'=>$id])
                ->order('log.create_time desc')
                ->group('coupon_id')
                ->page($page, $listRows)
                ->select();
            if(empty($list)){
                $list = [];
            }else{
                foreach ($list as $key => $item) {
                    $list[$key]['endtime'] = ['val'=>$item['endtime'], 'str'=>date('Y年m月d H:i:s', $item['endtime'])];
                }
            }
        }

        return $list;
    }

    // 一对一关联log表
    public function couponLog()
    {
        return $this->hasOne('CouponLog');
    }

    // 查询一条优惠券
    public static function getCouponOne($coupon_id)
    {
        $id = Token::getUidFromCache();
        // 查询优惠券数量，和领取数量，优惠券发放是否过期
        $couponOne = (new self())->field('id, number, end_time, status, take_count, valid_start_time, valid_end_time')->where('id', $coupon_id)->find();
        return $couponOne;
    }


}